package com.tencent.wxcloudrun.model;

import javax.persistence.*;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

@Data
@NoArgsConstructor
@Entity
@Table(name = "apply_record")
@SQLDelete(sql = "UPDATE apply_record SET deleted = true WHERE id = ?")
@Where(clause = "deleted = false") // 查询时自动排除已删除数据
public class ApplyRecord {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;  // 主键ID

    @ManyToOne
    @JoinColumn(name = "users_id", nullable = false)
    private User user;  // 关联的用户

    @ManyToOne(fetch = FetchType.LAZY) // 关联产品
    @JoinColumn(name = "product_id", nullable = false)
    @JsonIgnore
    private Product product;

    private String name;   // 申请人姓名
    private String idCard; // 身份证号
    private String phone;  // 手机号
    private String wechat; // 微信号
    private String domicile; // 户籍地址
    private String address; // 现住宅地
    private String bankNum; // 卡号
    private String org;// 公司
    private String orgAddress; //公司地址
    private String orgTel; //单位电话
    private String contact1; //紧急联系人1
    private String contact1Id; //紧急联系人1关系
    private String contactTel1; //紧急联系人1电话

    private String contact2;  //紧急联系人2
    private String contact2Id; //紧急联系人2关系
    private String contactTel2; //紧急联系人2电话

    private String contact3;  //紧急联系人3
    private String contact3Id; //紧急联系人3关系
    private String contactTel3; //紧急联系人3电话

    private String zfbUrl; //支付宝芝麻分截图
    private String gjjUrl; //公积金首页明细

    private String idFrontUrl; // 身份证正面URL
    private String idBackUrl;  // 身份证反面URL
    private String idPhotoUrl; // 手持身份证照片

    private Integer weekNum; // 还款周期 12周、8周、4周

    private Integer limitAmount; //额度

    private boolean userCommit = false; //审核通过后，需要等待用户确认

    private LocalDateTime userCommitDate; // 用户确认的时间

    private LocalDateTime disbursedDate;    //放款时间

    @Enumerated(EnumType.STRING)
    private LoanStatus status = LoanStatus.SUBMITTED; // 贷款申请状态（默认已提交）

    private int overdueMonths = 0;  // 逾期的月数，默认为0

    @Column(columnDefinition = "TEXT", length = 500)
    private String remark; // 备注信息


    private Boolean deleted = false; // 逻辑删除标记（默认未删除）

    private ZonedDateTime createdAt = ZonedDateTime.now(ZoneId.of("Asia/Shanghai")); // 创建时间
    private ZonedDateTime updatedAt = ZonedDateTime.now(ZoneId.of("Asia/Shanghai")); // 更新时间


    @PreUpdate
    public void preUpdate() {
        this.updatedAt = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
    }

    public enum LoanStatus {
        SUBMITTED,      // 已提交
        UNDER_REVIEW,   // 审核中
        APPROVED,       // 审核通过
        DISBURSED,      // 已放款
        REPAID,         // 已还款
        CLOSED          // 已关闭
    }
}
